package wifiunion.com.notice.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;


import java.io.IOException;

import wifiunion.com.notice.R;

/**
 * Created by Administrator on 2017/11/9.
 */

public class PlaySoundUtils {

    private static MediaPlayer mediaPlayer;

    /**
     * 获取播放器
     *
     * @return
     */
    private static MediaPlayer getMediaPlayer() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        } else {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.release();
            }
            mediaPlayer = null;
            mediaPlayer = new MediaPlayer();
        }
        return mediaPlayer;
    }

    /**
     * 结束播放器正在播放的内容
     */
    public static void releaseMedia() {
        try {
            if (mediaPlayer != null) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                } else {
                    mediaPlayer.release();
                }
                mediaPlayer = null;
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
            mediaPlayer = null;
        }
    }


    /**
     * 播放本地声音
     *
     * @param context
     * @param status
     */
    public static void playLocalSound(Context context, int status) {
        String packagename = context.getPackageName();
        mediaPlayer = getMediaPlayer();
        try {
            Uri uri;
            switch (status) {
                case 0:
//                    Log.e(Constants.TAG,"danger");
                    uri = Uri.parse("android.resource://" + packagename + "/" + R.raw.danger);
                    mediaPlayer.setDataSource(context, uri);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    break;
                case 1:
//                    Log.e(Constants.TAG,"normal");
                    uri = Uri.parse("android.resource://" + packagename + "/" + R.raw.normal);
                    mediaPlayer.setDataSource(context, uri);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    break;
                case 2:
//                    Log.e(Constants.TAG,"stranger");

                    uri = Uri.parse("android.resource://" + packagename + "/" + R.raw.stranger);
                    mediaPlayer.setDataSource(context, uri);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    break;
                default:
//                    Log.e(Constants.TAG,"default");
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
            mediaPlayer = null;
        }
    }


    /**
     * @param url
     */
    public static void playSoundUrl(String url) {

        mediaPlayer = getMediaPlayer();
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
            mediaPlayer = null;
        }

    }

}
